package com.rj.bd.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.rj.bd.entity.Book;
import com.rj.bd.entity.Dingdan;
import com.rj.bd.entity.User;
import com.rj.bd.service.IBookSerivce;
import com.rj.bd.service.IDingdanService;
import com.rj.bd.service.IUserService;
import com.rj.bd.utils.Dates;

import cn.hutool.core.util.IdUtil;
/**
 * @desc订单
 * @author 
 *
 */
@RestController
@ResponseBody
@CrossOrigin
public class DingdanController {

	@Autowired
	private IDingdanService dingdanService;
	
	@Autowired   //注入
	public IUserService userService;
	
	@Autowired   //注入
	public IBookSerivce bookSerivce;
	
	/**
	 * 查询全部
	 */
	@RequestMapping(value="/dingdan/queryAll")	
	public Map<String, Object>queryAll(){
	System.out.println("DingdanController---->queryAll()");
	Map<String, Object> map = new HashMap<String, Object>();
	
	List<Dingdan> list = dingdanService.queryAll();
	
	map.put("code", 0);
    map.put("msg", "请求成功");
    map.put("data", list);
    return map;
	}
	
	/**
     * 分页&条件查询（管理员用）  分页+定单号
     */
     @RequestMapping("/dingdan/queryByAdmin")  
     public Map<String,Object> queryByAdmin(@RequestParam Map maps, String did){
     System.out.println("DingdanController--->queryByAdmin()");
     Map<String, Object> map = new HashMap<String, Object>();
     System.out.println("did---"+did);
     
     List<Map> Page=dingdanService.getPage(maps,did);
     List<Dingdan>list=dingdanService.query(did);
     int count=list.size();
     System.out.println("count:"+count);

     
     map.put("count",count);
     map.put("code", 0);
     map.put("msg", "获取信息成功");
     map.put("data", Page);
     return map;
     
     }
	
     /**
      * 分页&条件查询（学会用）  分页+定单号
      */
      @RequestMapping("/dingdan/queryByUser")  
      public Map<String,Object> queryByUser(@RequestParam Map maps,String did,String uid){
      System.out.println("DingdanController--->queryByUser()");
      Map<String, Object> map = new HashMap<String, Object>();
      System.out.println("did---"+did);
      
      List<Map> Page=dingdanService.getPageByUser(maps,did,uid);
      System.out.println("Page:"+Page.toString());
      List<Map>list=dingdanService.queryByUser(maps,did,uid);
      int count=list.size();
      System.out.println("count:"+count);

      map.put("count",count);
      map.put("code", 0);
      map.put("msg", "获取信息成功");
      map.put("data", Page);
      return map;
      }
	
      /**
       * 添加订单(借书)
       * @return
       */
      @RequestMapping("/dingdan/add")
      public Map<String, Object> add(@RequestParam("uid")String uid, @RequestParam("bid")String bid, @RequestParam("dendtime")String dendtime) {
          System.out.println("DingdanController--->add()");
          Map<String, Object> map = new HashMap<String, Object>();

          System.out.println("uid:"+uid+"\t"+"bid:"+bid+"\t"+"dendtime:"+dendtime);

          Dingdan dingdan=new Dingdan();
          dingdan.setDid(IdUtil.simpleUUID());
          User user1=new User();
          user1.setUid(uid);
          dingdan.setUser(user1);
          Book book=new Book();
          book.setBid(bid);
          dingdan.setBook(book);
          
          dingdan.setDbegintime(Dates.CurrentTime3());//年月日 小时
          System.out.println(Dates.CurrentTime3());
          
          dingdan.setDendtime(dendtime);
          dingdan.setZhuangtai("1");
          
          //查看用户状态
          List<User> list=userService.queryByUid(uid);
          
          String zhuangtai=null;
          for (User user : list) {
  			System.out.println("用户状态为:"+user.getZhuangtai());
  			zhuangtai=user.getZhuangtai();
  		}
          
          if(zhuangtai=="2" || zhuangtai.equals('2')){
          	map.put("code", 0);
              map.put("msg", "借书失败");
              return map;
          }
          else{
        	  dingdanService.add(dingdan);
              map.put("code", 0);
              map.put("msg", "借书成功");
              return map;
          }
      }

	
      /**
       * 订单查看用户是否又逾期未还书
     * @throws ParseException 
       */
      @RequestMapping("/dingdan/queryByZhuangtai")
      public Map<String, Object> queryByZhuangtai(@RequestParam("uid")String uid) throws ParseException {
          System.out.println("DingdanController--->queryByZhuangtai()");
          Map<String, Object> map = new HashMap<String, Object>();
          System.out.println("uid:"+uid);
          List<Dingdan> list=dingdanService.queryByZhuangtai(uid);
          	
          
          
          String  zutime = null;
          String  huantime= null;
          for (Dingdan dingdan:list) {
              System.out.println(list);
              zutime=dingdan.getDbegintime(); 
              huantime=dingdan.getDendtime();
          }
          
          //判断时候逾期，逾期锁定用用户
          SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date date1 = format.parse(zutime);
          Date date2 = format.parse(huantime);
          //日期转时间戳（毫秒）
          long time1=date1.getTime();
          long time2=date2.getTime();
          long time3= time2 - time1;
          //time3=time3/3600000;  剩余几小时
          System.out.println("times:"+time3);
          	if(time3<0){
          		System.out.println("已逾期,账户被锁定");
          		dingdanService.editByUserZhuangtai(uid);
          	}
          
          map.put("code", 0);
          map.put("msg", "123");
          return map;
      }
      
      /**
       * 还书
       */
      @RequestMapping("/dingdan/editByZhuangTaiWei")
      public Map<String, Object> editByZhuangTaiWei(@RequestParam String bid){
          System.out.println("DingdanController--->editByZhuangTaiWei()");

          Map<String, Object> map = new HashMap<String, Object>();
          System.out.println("bid:"+bid);
          	
          bookSerivce.editByBookZhuangTaiWei(bid);
          
          dingdanService.editByDingdanZhuangTaiWei(bid);
          
          map.put("code", 0);
          map.put("msg", "还书成功");
          return map;
          
//          String zhuangtai=null;
//          for (User user : list) {
//  			System.out.println("用户状态为:"+user.getZhuangtai());
//  			zhuangtai=user.getZhuangtai();
//  		  }
          
//          if(zhuangtai=="2" || zhuangtai.equals('2')){
//          	map.put("code", 0);
//              map.put("msg", "借书失败");
//              return map;
//          }
//          else{
//          	bookSerivce.editByZhuangTaiWei(bid);
//              map.put("code", 0);
//              map.put("msg", "借书成功");
//              return map;
//          }
      }
      
	
	
}
